//
//  MZY_TableCollectionView.m
//  FullHouse
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_TableCollectionView.h"
#import "NetworkingTool.h"
#import "MZY_Discuss.h"
#import "MZY_TableCell.h"
#import "UIImageView+WebCache.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@implementation MZY_TableCollectionView

- (void)dealloc
{
    [self.urlStr release];
    [self.tableView release];
    [self.informationArr release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 70, WIDTH - 20, HEIGHT - 64 - 49 - 30) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor cyanColor];
    self.tableView.rowHeight = 130* HORIZONTAL;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MZY_TableCell class] forCellReuseIdentifier:@"table"];
    [self.contentView addSubview:self.tableView];
    [self.tableView release];

}

- (void)setUrlStr:(NSString *)urlStr{
    [self createData:urlStr];
}

- (void)createData:urlStr{
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.informationArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"topic_list"][@"topics"]) {
            MZY_Discuss *discuss = [[MZY_Discuss alloc]init];
            [discuss setValuesForKeysWithDictionary:temp];
            [self.informationArr addObject:discuss];
            [discuss release];
        }
        [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.informationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_TableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
    MZY_Discuss *discuss = self.informationArr[indexPath.row];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    if (discuss.pics.count != 0) {
        [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[discuss.pics lastObject][@"pic"]]];
    }else{
        cell.pictureImageView.image = [UIImage imageNamed:@"iconfont-tupian.png"];
    }
    cell.titleLabel.text = discuss.title;
    cell.messageImageView.image = [UIImage imageNamed:@"iconfont-xiaoxixuanzhong-2.png"];
    cell.messageLabel.text = discuss.reply_count;
    cell.viewImageView.image = [UIImage imageNamed:@"iconfont-01.png"];
    cell.viewLabel.text = discuss.view_count;
    [cell.authorImageView sd_setImageWithURL:[NSURL URLWithString:discuss.author_face]];
    cell.authorImageView.layer.cornerRadius = 20 * HORIZONTAL;
    cell.authorImageView.layer.masksToBounds = YES;
    cell.introduceLabel.text = discuss.reply_info[@"content"];
    cell.introduceLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

@end
